﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLKS2014.PUBLIC;
using System.Data;
using System.Windows.Forms;

namespace QLKS2014.DATA
{
    public class PhieuThuePhongData
    {
        DataConnecter connect;
        public PhieuThuePhongData()
        {
            connect = new DataConnecter();
        }

        //Thêm Phieu Thue Phong
        public void insert(PhieuThuePhongPublic phieuThuePhongPublic)
        {
            int param = 3;
            string[] name = new string[param];
            object[] values = new object[param];

            name[0] = "@NgayThue";
            name[1] = "@SoLuong";
            name[2] = "@MaPhong";


            values[0] = phieuThuePhongPublic.NgayThue;
            values[1] = phieuThuePhongPublic.SoLuong;
            values[2] = phieuThuePhongPublic.MaPhong;


            connect.getData("PhieuThuePhong_Insert", name, values, param);
        }

        //selectAll
        public List<PhieuThuePhongPublic> selectAll()
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhieuThuePhongPublic> list = new List<PhieuThuePhongPublic>();

                int row;
                dt = connect.getData("PhieuThuePhong_SelectAll");
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhieuThuePhongPublic phieuThuePhongPublic = new PhieuThuePhongPublic();
                    phieuThuePhongPublic.MaPhieuThue = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phieuThuePhongPublic.NgayThue = DateTime.Parse(dt.Rows[i].ItemArray[1].ToString());
                    phieuThuePhongPublic.SoLuong = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    phieuThuePhongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[3].ToString());

                    list.Add(phieuThuePhongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

        public List<PhieuThuePhongPublic> selectByMaxMaPhieuThue(PhieuThuePhongPublic phieuThuePhong)
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhieuThuePhongPublic> list = new List<PhieuThuePhongPublic>();
                int param = 1;
                string[] name = new string[param];
                object[] values = new object[param];

                name[0] = "@MaPhong";
                values[0] = phieuThuePhong.MaPhong;
                int row;
                dt = connect.getData("PhieuThuePhong_SelectByMaxMaPhieuThue",name,values,param);
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhieuThuePhongPublic phieuThuePhongPublic = new PhieuThuePhongPublic();

                    phieuThuePhongPublic.MaPhieuThue = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phieuThuePhongPublic.NgayThue = DateTime.Parse(dt.Rows[i].ItemArray[1].ToString());
                    phieuThuePhongPublic.SoLuong = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    phieuThuePhongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[3].ToString());

                    list.Add(phieuThuePhongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }





        public List<PhieuThuePhongPublic> selectByMaxMaPhieuThue_Tmp(PhieuThuePhongPublic phieuThuePhong)
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhieuThuePhongPublic> list = new List<PhieuThuePhongPublic>();
                int param = 1;
                string[] name = new string[param];
                object[] values = new object[param];

                name[0] = "@MaPhong";
                values[0] = phieuThuePhong.MaPhong;
                int row;
                dt = connect.getData("PhieuThuePhong_SelectByMaxMaPhieuThue_Tmp", name, values, param);
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhieuThuePhongPublic phieuThuePhongPublic = new PhieuThuePhongPublic();

                    phieuThuePhongPublic.MaPhieuThue = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phieuThuePhongPublic.NgayThue = DateTime.Parse(dt.Rows[i].ItemArray[1].ToString());
                    phieuThuePhongPublic.SoLuong = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    phieuThuePhongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[3].ToString());

                    list.Add(phieuThuePhongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }



        
    }
}